angular.module('Controller')

.controller('WICustomerManageEditCtrl', ($scope,NgTableParams,wfAlert,cryptUtil,$localStorage, commonUtil,menuManage,commonConstant,$rootScope,$uibModal,$http) ->
    if $rootScope.pageType and $rootScope.pageParams
        $scope.type=$rootScope.pageType
        $scope.data=angular.copy($rootScope.pageParams)
        $rootScope.pageType=undefined
        $rootScope.pageParams=undefined
    $scope.roleData=[]
    $scope.typeList=commonConstant.authorizationTypeList()
    $scope.enabledList=commonConstant.enabledList()
    $scope.sexList=commonConstant.sexList()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    if $scope.data.id
                        $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.resetData=()->
        $scope.data.authority=''
        $scope.data.description=''
        $scope.data.type=''
        $scope.data.enabled=''
    $scope.newData=()->
        postData={
            accountId:$localStorage['accountId']
            username:$scope.data.username
            enabled:$scope.data.enabled
        }
        if $scope.data.password
            postData.password=cryptUtil.hex_md5($scope.data.password)
        getParams=commonUtil.transferGetParam(postData)
        $http.post('@ostengar/customerUser',postData)
        .success((resp)->
            wfAlert.toastInfo('新增帐号成功')
#            if(resp.code==0)
#                console.log('新增帐号:',resp)
#                wfAlert.toastInfo('新增帐号成功')
#            else
#                wfAlert.toastError('新增帐号失败：',resp.message)
        )
    $scope.editData=()->
        putData={
            accountId:$localStorage['accountId']
            username:$scope.data.username
            enabled:$scope.data.enabled
        }
        if $scope.data.password
            putData.password=cryptUtil.hex_md5($scope.data.password)
        getParams=commonUtil.transferGetParam(putData)
        $http.put('@ostengar/customerUser/'+$scope.data.id,putData)
        .success((resp)->
            if(resp.code==0)
                console.log('修改帐号:',resp)
                wfAlert.toastInfo('修改成功')
            else
                wfAlert.toastError('修改失败：',resp.message)
        )
    $scope.confirm=()->
        if $scope.type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.queryData=(firstPage)->
        if(firstPage == true)
            $scope.tableParams.page(1)
        $scope.tableParams.reload()

)